package com.xuexi.bootstrap;



import com.xuexi.bootstrap.controller.Verse5;
import com.xuexi.bootstrap.controller.Verse7;
import com.xuexi.bootstrap.javadbf.DBFField;
import com.xuexi.bootstrap.javadbf.DBFReader;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;


/**
 * @Author 小年
 * @Date 2020/04/15 13:58
 */

@SpringBootApplication
public class BootstrapApplication {

    public static  List<Verse5>  verseList5=new ArrayList<>();
    public static  List<Verse7>  verseList7=new ArrayList<>();
    public static void main(String[] args) {
        //读取文件的地址
        readDBF("/home/ts-user/ts-service/兴理.DBF");
//        readDBF("src/main/resources/static/兴理.DBF");
        SpringApplication.run(BootstrapApplication.class, args);
    }

    /**
     * 读取dbf
     *
     * @param path
     */
    public static void readDBF(String path) {
        InputStream fis = null;
        try {
            // 读取文件的输入流
            fis = new FileInputStream(path);
            // 根据输入流初始化一个DBFReader实例，用来读取DBF文件信息
            DBFReader reader = new DBFReader(fis);
            // 解决乱码
            reader.setCharactersetName("GBK");
            // 调用DBFReader对实例方法得到path文件中字段的个数
            int fieldsCount = reader.getFieldCount();
            // 取出字段信息
            for (int i = 0; i < fieldsCount; i++) {
                DBFField field = reader.getField(i);
                System.out.println(field.getName());
            }
            Object[] rowValues;
            // 一条条取出path文件中记录
            //

            while ((rowValues = reader.nextRecord()) != null) {

                        if(  rowValues[3].toString().equals("5J")){
                            Verse5 verse5=new Verse5();
                            verse5.setAuthor(rowValues[0].toString());
                            String[] oneSentence  =rowValues[4].toString().split("/");
                            String[] twoSentence  =rowValues[5].toString().split("/");
                            String[] threeSentence  =rowValues[6].toString().split("/");
                            String[] fourSentence  =rowValues[7].toString().split("/");
                            verse5.setOneSentence1(oneSentence[0].trim());
                            verse5.setOneSentence2(oneSentence[1].trim());
                            verse5.setOneSentence3(oneSentence[2].trim());
                            verse5.setOneSentence4(oneSentence[3].trim());
                            verse5.setOneSentence5(oneSentence[4].trim());
                            verse5.setTwoSentence1(twoSentence[0].trim());
                            verse5.setTwoSentence2(twoSentence[1].trim());
                            verse5.setTwoSentence3(twoSentence[2].trim());
                            verse5.setTwoSentence4(twoSentence[3].trim());
                            verse5.setTwoSentence5(twoSentence[4].trim());
                            verse5.setThreeSentence1(threeSentence[0].trim());
                            verse5.setThreeSentence2(threeSentence[1].trim());
                            verse5.setThreeSentence3(threeSentence[2].trim());
                            verse5.setThreeSentence4(threeSentence[3].trim());
                            verse5.setThreeSentence5(threeSentence[4].trim());
                            verse5.setFourSentence1(fourSentence[0].trim());
                            verse5.setFourSentence2(fourSentence[1].trim());
                            verse5.setFourSentence3(fourSentence[2].trim());
                            verse5.setFourSentence4(fourSentence[3].trim());
                            verse5.setFourSentence5(fourSentence[4].trim());
                            verseList5.add(verse5);
                        }
                        if(  rowValues[3].toString().equals("7J")){
                            Verse7 verse7=new Verse7();
                            verse7.setAuthor(rowValues[0].toString());
                            String[] oneSentence  =rowValues[4].toString().split("/");
                            String[] twoSentence  =rowValues[5].toString().split("/");
                            String[] threeSentence  =rowValues[6].toString().split("/");
                            String[] fourSentence  =rowValues[7].toString().split("/");
                            verse7.setOneSentence1(oneSentence[0].trim());
                            verse7.setOneSentence2(oneSentence[1].trim());
                            verse7.setOneSentence3(oneSentence[2].trim());
                            verse7.setOneSentence4(oneSentence[3].trim());
                            verse7.setOneSentence5(oneSentence[4].trim());
                            verse7.setOneSentence6(oneSentence[5].trim());
                            verse7.setOneSentence7(oneSentence[6].trim());
                            verse7.setTwoSentence1(twoSentence[0].trim());
                            verse7.setTwoSentence2(twoSentence[1].trim());
                            verse7.setTwoSentence3(twoSentence[2].trim());
                            verse7.setTwoSentence4(twoSentence[3].trim());
                            verse7.setTwoSentence5(twoSentence[4].trim());
                            verse7.setTwoSentence6(twoSentence[5].trim());
                            verse7.setTwoSentence7(twoSentence[6].trim());
                            verse7.setThreeSentence1(threeSentence[0].trim());
                            verse7.setThreeSentence2(threeSentence[1].trim());
                            verse7.setThreeSentence3(threeSentence[2].trim());
                            verse7.setThreeSentence4(threeSentence[3].trim());
                            verse7.setThreeSentence5(threeSentence[4].trim());
                            verse7.setThreeSentence6(threeSentence[5].trim());
                            verse7.setThreeSentence7(threeSentence[6].trim());
                            verse7.setFourSentence1(fourSentence[0].trim());
                            verse7.setFourSentence2(fourSentence[1].trim());
                            verse7.setFourSentence3(fourSentence[2].trim());
                            verse7.setFourSentence4(fourSentence[3].trim());
                            verse7.setFourSentence5(fourSentence[4].trim());
                            verse7.setFourSentence6(fourSentence[5].trim());
                            verse7.setFourSentence7(fourSentence[6].trim());
                            verseList7.add(verse7);
                        }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }
}
